package own.stu.jobgib.playown.bigdata.minio;

import io.minio.*;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class MinioTest {

    public static void main(String[] args) throws Exception {

        MinioClient client = MinioClient.builder()
                .endpoint("http://127.0.0.1:9000")
                .credentials("ROOT", "12345678")
                .build();

        String bucketName = "test-minio";

        boolean bucketExists = client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (bucketExists) {
            System.out.println(" ----- " + bucketName + "bucket exist");
        } else {
            client.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }


        /*client.uploadObject(UploadObjectArgs.builder().bucket(bucketName)
                .object("治愈-晚霞.mp4")
//                .filename("/Users/qim/Desktop/bili/510805858/2-故乡连绵秋雨｜大雾弥漫｜蓝天白云｜治愈 晚霞.mp4") // not a regular file
//                .filename("1.txt")
                .filename("target/classes/xxx.yml")
                .contentType("video/mp4")
                .build());*/
        //上传已经长度的流
        /*File f = Paths.get("/Users/qim/Desktop/bili/510805858/1-故 乡 秋 天.jpg").toAbsolutePath().normalize().toFile();
        InputStream inputStream =new FileInputStream(f);
        client.putObject(
                PutObjectArgs.builder().bucket(bucketName)
                        .object("-故 乡 秋 天.jpg").stream(inputStream, f.length(), -1)
                        .contentType("image/jpg")
                        .build());

        //上传未知长度的流
        client.putObject(
                PutObjectArgs.builder().bucket(bucketName)
                        .object("test3.jpg").stream(inputStream, -1, 10485760)
                        .contentType("image/jpg")
                        .build());*/

        File f1 = Paths.get("/Users/qim/Desktop/bili/510805858/8-“好雨知时节，当春乃发生。”.mp4").toAbsolutePath().normalize().toFile();
        File f2 = Paths.get("/Users/qim/Desktop/bili/510805858/9-“自古逢秋悲寂寥，我言秋日胜春朝。”.jpg").toAbsolutePath().normalize().toFile();
        List<SnowballObject> objects = new ArrayList<>();
        objects.add(
                new SnowballObject(
                        "data-01.mp4",
                        new FileInputStream(f1),
                        f1.length(),
                        null));
        objects.add(
                new SnowballObject(
                        "data-02.jpg",
                        new FileInputStream(f2),
                        f2.length(),
                        null));
        client.uploadSnowballObjects(
                UploadSnowballObjectsArgs.builder().bucket(bucketName).objects(objects).build());

        System.out.println("done");

        // https://blog.csdn.net/qq_22885233/article/details/122829045
    }
}
